package com.sfac.springCloudTest.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.sfac.springCloudTest.dao.CityDao;
import com.sfac.springCloudTest.service.CityService;
import org.example.common.vo.Result;
import org.example.common.vo.Search;
import org.example.test.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author jzq
 * @date 2022/8/18 9:38
 */
@SuppressWarnings({"all"})
@Service
public class CityServiceImpl implements CityService {

    @Value("${server.port}")
    private int serverPort;

    @Autowired
    private CityDao cityDao;


    @Override
    @Transactional
    public Result<City> insertCity(City city) {
        City temp = cityDao.getCityByCityName(city.getCityName());
        if (temp != null) {
            return new Result<>(Result.ResultStatus.FAILED.code,
                    "City name is repead");
        }

        city.setDateCreated(new Date());
        city.setDateModified(new Date());

         cityDao.insertCity(city);
        return new Result<>(Result.ResultStatus.SUCCESS.code,
                "Insert success", city);
    }


    @Override
    @Transactional
    public Result<City> updateCity(City city) {
        City temp = cityDao.getCityByCityName(city.getCityName());
        if (temp != null && temp.getCityId() != city.getCityId()) {
            return new Result<>(Result.ResultStatus.FAILED.code,
                    "City name is repead");
        }

        city.setDateModified(new Date());
         cityDao.updateCity(city);
        return new Result<>(Result.ResultStatus.SUCCESS.code,
                "Update success", city);
    }


    @Override
    @Transactional
    public Result<Object> deleteCityByCityId(int cityId) {
         cityDao.deleteCityByCityId(cityId);
        return new Result<>(Result.ResultStatus.SUCCESS.code,
                "Delete success");
    }


    @Override
    public City getCityByCityId(int cityId) {
        System.out.println(serverPort);
        return cityDao.getCityByCityId(cityId);
    }


    @Override
    public City getCityByNames(String cityName, String localCityName) {
        System.out.println(serverPort);
        return cityDao.getCityByNames(cityName, localCityName);
    }


    @Override
    public List<City> getCitiesByCountryId(int countryId) {
        return Optional.ofNullable(cityDao.getCitiesByCountryId(countryId))
                .orElse(Collections.emptyList());
    }


    @Override
    public PageInfo<City> getCitiesBySearch(Search search) {
        //初始化search对象
        search.init();
        //分页
        PageHelper.startPage(search.getCurrentPage(), search.getPageSize());
        //返回分页对象
        return new PageInfo<>(Optional
                .ofNullable(cityDao.getCitiesBySearch(search))
                .orElse(Collections.emptyList()));
    }

}
